/**
 *  文件名:RegisterActivity.java
 *  修改人:lihangjie
 *  创建时间:2015-9-3 下午5:03:08
 */
package com.sharemob.tinchat.modules.account;

import java.util.Timer;
import java.util.TimerTask;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import cn.smssdk.SMSSDK;

import com.sharemob.tinchat.R;
import com.sharemob.tinchat.base.BaseActivity;
import com.sharemob.tinchat.base.Callback;
import com.sharemob.tinchat.base.ClearEditText;
import com.sharemob.tinchat.modules.dao.cache.CacheManager;
import com.sharemob.tinchat.modules.launch.MainActivity;

/**
 * 
 * <一句话功能简述>
 *
 * @author lihangjie
 * version [版本号,2015-9-3 下午5:03:08]
 * @see    [相关类/方法]
 * @since  [产品/模块版本]
 *
 */
public class RegisterActivity extends BaseActivity {

	private static final String ShareMob_AppKey="a8a336e62ff2";
	private static final String ShareMob_AppSecret="05a4f7e86010f0ed5adf7b7802251d1b";
	//间隔发送验证码
	private static final int INTERVAL_SEND_CODE=10;
	private ClearEditText et_verification_code;
	private ClearEditText et_account_phone;
	private ClearEditText et_account_pwd;
	private Button btn_send_code;
	private Timer timer; 
	private String code;
	private Handler handler;
	private int count=INTERVAL_SEND_CODE;
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_register);
        
		handler = new Handler(){
			public void handleMessage(Message msg) {  
                super.handleMessage(msg);  
            	if (count>0) {  
                    count--;
                    btn_send_code.setBackgroundResource(R.drawable.style_btn_send_code_disable);
                    btn_send_code.setText(String.format(" %s秒后重试", String.valueOf(count)));
                }else{  
                	timer.cancel();
                	btn_send_code.setBackgroundResource(R.drawable.style_btn_send_code);
                	btn_send_code.setEnabled(true);
                	count=INTERVAL_SEND_CODE;
                	btn_send_code.setText(String.format("获取验证码")); 
                }
			};
		};
		//初始化
        SMSSDK.initSDK(this,ShareMob_AppKey, ShareMob_AppSecret,true);
//        EventHandler eh=new EventHandler(){
//        	 
//            public void afterEvent(int event, int result, Object data) {
// 
//               if (result == SMSSDK.RESULT_COMPLETE) {
////            	   showToast("RESULT_COMPLETE");
//                //回调完成
//                if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
//             	   showToast("EVENT_SUBMIT_VERIFICATION_CODE");
//                //提交验证码成功
//                }else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){
//           
////					HashMap<String,Object> phoneMap = (HashMap<String, Object>) data;
////					String code = (String) phoneMap.get("phone");
//			     	showToast("EVENT_GET_VERIFICATION_CODE:");
//                //获取验证码成功
//                }else if (event ==SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES){
////                	showToast("EVENT_GET_SUPPORTED_COUNTRIES");
//                //返回支持发送验证码的国家列表
//                } 
//              }else{                                                                 
//                 ((Throwable)data).printStackTrace(); 
//          }
//      } 
//   }; 
//   SMSSDK.registerEventHandler(eh); //注册短信回调
        exitView(R.id.title_back, R.anim.activity_close,0);
        //同意协议
        TextView tv_agree_agreement=(TextView)findViewById(R.id.tv_agree_agreement);
        String html ="<span color='#595959'>点击下一步表示同意<font color='#00a8ff'>TinChat软件许可及服务协议</font></span>";
        CharSequence charSequence = Html.fromHtml(html);        
        tv_agree_agreement.setText(charSequence);
        //监听协议事件
        BtnEvent(R.id.tv_agree_agreement, new Callback() {
			public void doDown() {
				gotoViewByAnim(RegisterActivity.this, UserProtocolActivity.class, R.anim.in_from_bottom,false);
			}
		});
        
        et_verification_code=(ClearEditText)findViewById(R.id.et_verification_code);
        
        
        et_account_phone=(ClearEditText)findViewById(R.id.et_account_phone);
        
        
        et_account_pwd=(ClearEditText)findViewById(R.id.et_account_pwd); 
        
        btn_send_code=(Button)findViewById(R.id.btn_send_verification_code);
        timer = new Timer(); 
        btn_send_code.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {
            	String account_phone=et_account_phone.getText().toString();
            	
//				String verification_code=et_verification_code.getText().toString();
            	
            	if(et_account_phone.getText().toString().length()==0){
					showToast("请输入手机号码!");
				}
				else if(et_account_phone.getText().toString().length()==11){
					   if(count==INTERVAL_SEND_CODE){
						    cn.smssdk.SMSSDK.getVerificationCode("86",account_phone);
				        	timer=new Timer();
				        	btn_send_code.setEnabled(false);
				            TimerTask task = new TimerTask(){    
					            public void run() {  
					                Message message = new Message();      
					                message.what = 1;      
					                handler.sendMessage(message);    
					            }            
					        };  
				        	timer.schedule(task, 1000,1000);
				        }
				}
		     
            }
        });
        
//        BtnEvent(R.id.btn_send_verification_code, new Callback() {
//			public void doDown() {
////				code=Utils.getVerificationCode();
//	
//		        //发送短信
//				
//		        
//			}
//		});
        
        BtnEvent(R.id.title_submit, new Callback() {
			public void doDown() {
				if(et_account_phone.getText().toString().length()==0){
					showToast("请输入手机号码!");
				}else if(et_verification_code.getText().toString().length()==0){
					showToast("验证码不能为空!");
				}else if(et_account_pwd.getText().toString().length()==0){
					showToast("密码不能为空!");
				}else{
//					String verification_code=et_verification_code.getText().toString();
//					String account_phone=et_account_phone.getText().toString();
//					String data=String.format("appkey=%s&phone=%s&zone=86&code=%s",ShareMob_AppKey,account_phone ,verification_code);
//					Utils.send("https://api.sms.mob.com/sms/verify", data, false, RegisterActivity.class.getName(), "submit");
				  
				  CacheManager.getInstance().RegisterPhoneLogin();
				  Intent sintent = new Intent(RegisterActivity.this, MainActivity.class);
    		      startActivity(sintent);
    		      overridePendingTransition( R.anim.in_from_zoom,0);
				
				}
			}
        });
    }
    
    
    
    @Override
    protected void onDestroy() {
    	super.onDestroy();
    	timer.cancel();
    	handler=null;
    }
    
    public void submit(String body){
    	showToast(body);
    }
    
	protected void keyEventOfListView(int position, int id) {
		
	}

}
